import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mo.fei on 2015/9/26.
 */
public class JsoupDemo {
    public void mySpider() throws IOException {
        Document doc = Jsoup.connect("http://www.xunleigang.com/forum.php?mod=forumdisplay&fid=2&ortid=1&sortid=1&page=1&mobile=no").get();
        Elements nums=doc.select(".last");
        System.out.println("total:" + nums.get(0).text());
        Elements links = doc.select("a[href^=thread]");
        List<Integer> validList=new ArrayList<Integer>();
        boolean flag=true;
        for (int i = 0; i <links.size() ; i++) {
            if(i==links.size()-1){
                continue;
            }
            if(links.get(i).attr("href")!=null&&!links.get(i).attr("href").trim().equals("")){
                if(links.get(i).attr("href").trim().equals(links.get(i+1).attr("href"))){
                    if(flag){
                        validList.add(i);
                        links.set(i + 1, links.get(i));
                        flag=false;
                    }
                }else{
                    flag=true;
                }
            }
        }
        System.out.println(validList.size());
        for (Integer num : validList) {
            System.out.println("name:"+links.get(num).text()+",href:"+links.get(num).attr("href"));
        }
    }

    public static void main(String[] args) throws IOException {
        JsoupDemo demo=new JsoupDemo();
        demo.mySpider();
    }
}
